package com.lzp.appexp

import kotlinx.coroutines.*
import kotlinx.coroutines.channels.ReceiveChannel
import kotlinx.coroutines.channels.consumeEach
import kotlinx.coroutines.channels.produce
import kotlinx.coroutines.flow.*
import java.lang.Exception
import kotlin.system.measureTimeMillis

/**
 * @describe:
 * @Author: lixiaopeng
 * @Date: 4/23/21
 */

fun CoroutineScope.produceSquares(): ReceiveChannel<Int> = produce {
    for (x in 1..5) send(x * x)
}

/*
fun main() = runBlocking {
    val squares = produceSquares()
    squares.consumeEach { println(it) }
    println("Done!")
}*/

fun main() = runBlocking {
    test()
    println("")

    delay(1000)
    test()
    println("")

    delay(1000)
    test()
    println("")

    delay(1000)
    test()
    println("")

    delay(1000)
    test()
    println("")
}

inline fun test()= runBlocking {
    launch (context = Dispatchers.IO){
        println("launch 1 start ${Thread.currentThread().name}")
        println("launch 1 end")
    }
    launch (){
        println("launch 2 start ${Thread.currentThread().name}")
        println("launch 2 end")
    }
    println("done ${Thread.currentThread().name}")
}